package com.ycy;

import org.apache.zookeeper.*;

import java.io.IOException;
import java.util.List;
import java.util.concurrent.locks.ReentrantLock;

public class ProducerConsumer {

  public static void main(String[] args) {

    ReentrantLock reentrantLock = new ReentrantLock();
    reentrantLock.lock();

    reentrantLock.unlock();

  }

  static class Producer {

    ZooKeeper zooKeeper;

    public Producer() {

      try {
        zooKeeper = new ZooKeeper("", 5000, new Watcher() {
          @Override
          public void process(WatchedEvent event) {
            //事件通知机制
            System.out.println(event);
          }
        });
      } catch (IOException e) {
        e.printStackTrace();
      }

    }

    public void producer() {
      while (true) {
        // 8s 生产一个
        try {
          int max = 10;
          List<String> children = zooKeeper.getChildren("/ycy/queue", false);
          while (children.size() >= 10) {
            // wait

          }
          //持久化 有序znode
          zooKeeper.create("/ycy/queue/child", null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL);
        } catch (KeeperException e) {
          e.printStackTrace();
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
      }
    }

  }

  static class Consumer {

    public void consumer() {

    }

  }


}
